package com.demo.hdfs.httpfs;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jdk.nashorn.internal.parser.JSONParser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Set;

public class HttpFSUtils {
	private final static Log log = LogFactory.getLog(HttpFSUtils.class);
	private static final String SERVICE_PATH = "/webhdfs/v1";
	private static final String SCHEME = "http";

	public static String createURL(String path, String params) {
		StringBuilder sb = new StringBuilder();
		sb.append(SCHEME).append("://").append(HttpFSConf.getHOST())
				.append(":").append(HttpFSConf.getPORT()).append(SERVICE_PATH)
				.append(path).append("?").append(params);
		return sb.toString();
	}

//	public static void validateResponse(InputStream in, int respCode,
//			int expectedCode) throws Exception {
//		if (respCode != expectedCode) {
//			JSONObject json = JSONObject.parseObject(in, JSONObject.class);
//			json = (JSONObject) json.get("RemoteException");
//			String message = (String) json.get("message");
//			String exception = (String) json.get("exception");
//			String className = (String) json.get("javaClassName");
//			throw new RuntimeException(message);
//		}
//	}

//	public static void parseResult(String str) {
//		try {
//			JSONParser parser = new JSONParser();
//			JSONObject json =  parser.parse(str);
//			json = (JSONObject) json.get("FileStatuses");
//			JSONArray jsonArray = (JSONArray) json.get("FileStatus");
//			for (int i = 0; i < jsonArray.size(); i++) {
//				JSONObject obj = (JSONObject) jsonArray.get(i);
//				Set set = obj.keySet();
//				for (Iterator it = set.iterator(); it.hasNext();) {
//					String key = (String) it.next();
//					log.info(key + "==>" + obj.get(key));
//				}
//			}
//		} catch (Exception e) {
//			log.error("jsonParse fail", e);
//		}
//	}



}